﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Reborn_WorldServer.App1AppHandler.Spells.Interfaces;
using App1AppHandler;
using System.Drawing;

namespace Reborn_Server.App1AppHandler
{
    public class Spell_CrystallineWebAll : TargetSpell, IInstantCast
    {
        public void Resume(CreatureBase targetCreatureBase, SpellIcon _spellIcon, int timeRemaining, int castLevel, int castCharacterID, int castInt, int castWis, int optionalValue, string xmlData)
        {
            //Resume a normal stun with this spell icon.

            Spell_CrystallineWeb cwSpell = new Spell_CrystallineWeb();
            cwSpell.Resume(targetCreatureBase, _spellIcon, timeRemaining, castLevel, castCharacterID, castInt, castWis, optionalValue, xmlData);
        }

        public void InstantCast(CreatureBase castingCreature, CreatureBase targetCreatureBase, SpellIcon _spellIcon, int castLevel)
        {
            if (base.InitializeInstantCast(castingCreature, targetCreatureBase, _spellIcon, castLevel, false) == false)
                return;

            List<Point> hiddenList = CreatureAI.FindHidden(castingCreature, castingCreature.currentMapItem.mapItemsWithin4, false);

            List<CreatureBase> creaturesToEffect = new List<CreatureBase>();

            List<MapItem> animateMapItems = new List<MapItem>();
            foreach (MapItem mapItem in castingCreature.currentMapItem.mapItemsWithin4)
            {
                if (CreatureAI.isHidden(mapItem.MapX, mapItem.MapY, hiddenList) == false)
                {
                    bool foundCreature = false;
                    foreach (CreatureBase creatureBase in mapItem.CreaturesBases)
                    {
                        if (creatureBase.HP > 0 &&
                            (creatureBase.Faction == TargetCreatureBase.Faction || castingCreature.hateManager.GetCreatureHateInfo(creatureBase) != null) && 
                            creatureBase.CanSeeMe(castingCreature))
                        {
                            foundCreature = true;
                            creaturesToEffect.Add(creatureBase);
                        }
                    }

                    if (foundCreature == true)
                        animateMapItems.Add(mapItem);
                }
            }

            List<CreatureBase> creatureBasesToUpdate = new List<CreatureBase>();

            //Damage them later, damaging them can cause creatures to be removed from the tile which will break the above iteration
            foreach (CreatureBase creaturebase in creaturesToEffect)
            {
                //If we're attacking a player, and we dont have a flag on them, give them a flag on us.
                if (creaturebase is Character &&
                    castingCreature is Character &&
                    creaturebase != castingCreature &&
                    castingCreature.hateManager.GetCreatureHateInfo(creaturebase) == null &&
                    spellIcon.Offensive == true)
                {
                    Character charToAttack = (Character)creaturebase;
                    if (charToAttack.friendsManager.hasNoSDFlag(CastingCharacterID) == false)
                    {
                        if (creaturebase.hateManager.GetCreatureHateInfo(castingCreature) == null)
                        {
                            creaturebase.SendMessage("You have been ATTACKED by " + castingCreature.Name + "!", "damaged");
                            creaturebase.PlaySound("Ding", creaturebase.MapX, creaturebase.MapY);

                            creaturebase.hateManager.UpdateList(castingCreature, 0);


                            charToAttack.AddMapItem(castingCreature.currentMapItem);
                        }
                        else
                            creaturebase.hateManager.UpdateList(castingCreature, 0);
                    }
                }

                Spell_CrystallineWeb buff = new Spell_CrystallineWeb();
                if (buff.tryResist(this.spellIcon, CastingCreatureLevel, CastingCreatureInt, CastingCreatureWis, creaturebase) == true)
                {
                    if (creaturebase != castingCreature)
                        castingCreature.SendMessage(creaturebase.Name + " resists " + spellIcon.Name + "!", "spelldefense");

                    creaturebase.SendMessage("You resist " + castingCreature.Name + "'s " + spellIcon.Name + "!", "spelloffense");
                }
                else
                {
                    Buff oldBuff = creaturebase.buffManager.GetBuffByStackType(spellIcon.StackType);
                    if (oldBuff != null)
                        oldBuff.RemoveSpell(false, false);

                    CreatureBase cb = creaturebase;
                    buff.Resume(cb, this.spellIcon, -1, CastingCreatureLevel, CastingCharacterID, CastingCreatureInt, CastingCreatureWis, -1, null);
                    cb.buffManager.UpdateBuffs();

                    if (creaturebase != castingCreature)
                        castingCreature.SendMessage(creaturebase.Name + " is stuck!", "spelldefense");

                    creaturebase.SendMessage("You are stuck in " + castingCreature.Name + "'s " + spellIcon.Name + "!", "spelloffense");

                    creatureBasesToUpdate.Add(creaturebase);
                }
            }

            List<Character> charactersWithin8 = castingCreature.currentMapItem.getCharactersWithin8();
            foreach (Character character in charactersWithin8)
            {
                foreach (MapItem mapItem in animateMapItems)
                    character.AddAnimation(spellIcon.FieldImageURL, TargetCreatureBase.MapID, mapItem.MapX, mapItem.MapY, 1);

                foreach (CreatureBase cb in creatureBasesToUpdate)
                    character.AddCreatureBase(cb);
            }

        }
    }
}
